package com.blankland.dataanalysis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blankland.dataanalysis.domain.User;
import com.blankland.dataanalysis.domain.vo.LoginVO;
import com.blankland.dataanalysis.exception.GuiguException;
import com.blankland.dataanalysis.result.Result;
import com.blankland.dataanalysis.service.MenuService;
import com.blankland.dataanalysis.service.RoleMenuService;
import com.blankland.dataanalysis.service.UserRoleService;
import com.blankland.dataanalysis.service.UserService;
import com.blankland.dataanalysis.utls.JwtHelper;
import com.blankland.dataanalysis.utls.MD5;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * vue-admin-template登录模块对应的后台代码
 *
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class LoginController {


    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private UserRoleService userRoleService;


    /**
     * 用户登录
     *  1、根据username查询User对象
     *  2、判断User对象是否为null
     *  3、判断用户输入的密码和数据库中md5加密之后的密码是否一致
     *  4、判断当前用户是否可用
     *  5、使用JwtHelper生成token
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO){
        //1、根据username查询数据
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", loginVO.getUsername());
        User user = userService.getOne(wrapper);

        //2、判断结果是否为空
        if (user == null){
            throw new GuiguException(20001,"用户不存在");
        }

        //3、判断密码是否一致（数据库中MD5加密）
        String password = loginVO.getPassword();
        if (!user.getPassword().equals(MD5.encrypt(password))){
            throw new GuiguException(20002,"密码错误");
        }

        //4、判断当前用户状态是否可用
        if (user.getStatus() == 0){
            throw new GuiguException(20003,"用户已被禁用");
        }

        //5、根据userid和username生成token
        int id = Long.valueOf(user.getId()).intValue();
        String token = JwtHelper.createToken(id, user.getUsername());

        //6、返回map
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);

        //7、返回token信息
        return Result.ok(map);
    }

    /**
     * 用户登录成功，获取请求头中的token数据
     *      1、获取请求头中的token字符串
     *      2、解析token，获取里面的username
     *      3、根据用户名称，获取用户的信息（基本信息+菜单权限+按钮权限）
     * @return
     */
    @GetMapping("/info")
    public Result info(HttpServletRequest request){
        //1、获取请求头中的token字符串
        String token = request.getHeader("token");
        //2、解析token，获取里面的username
        String username = JwtHelper.getUsername(token);
        System.out.println(username);
        //3、根据username，获取用户基本信息+菜单权限+按钮权限
        Map<String,Object> map = userService.getUserInfo(username);
        return Result.ok(map);
    }


    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }


}
